#include <stdio.h>
#include <stdlib.h>
#include "triangle.h"
#include "triangle_list.h"


TriangleList * newTriangleList(){
	TriangleList * el = (TriangleList *)malloc(sizeof(TriangleList));
	el->triangles = NULL;
	el->length = 0;
	return el;
}

Triangle * addTriangleToList(Triangle * list, Triangle * e){
	if(list == NULL){
		e->next = NULL;
		return e;
	}
	else{
		list->next = addTriangleToList(list->next,e);
		return list;
	}
}

Triangle * addAllTrianglesToList(Triangle * list, Triangle * e){
	if(list == NULL){
		return e;
	}
	else{
		list->next = addAllTrianglesToList(list->next,e);
		return list;
	}
}

Triangle * removeTriangleFromList(Triangle * list, Triangle * e){
	if(list != NULL){
		if(compareTriangle(list,e)){
			return list->next;
		}
		else{
			list->next = removeTriangleFromList(list->next,e);
			return list;
		}
	}
	return list;
}

Triangle * removeTriangleByIndex(Triangle * list, int index){
	if(list != NULL){
		if(index == 0){
			return list->next;
		}
		else{
			index--;
			list->next = removeTriangleByIndex(list->next,index);
			return list;
		}
	}
	return list;
}

int containsTriangleInList(Triangle * list, Triangle * e){
	if(list != NULL){
		if(compareTriangle(list,e)){
			return 1;
		}
		else{
			return containsTriangleInList(list->next,e);
		}
	}
	else
		return 0;
}

void printTriangleList(TriangleList * el){
	Triangle * e = el->triangles;
	int index=0;
	printf("TriangleLIST\n===============\n");
	while(e != NULL){
		printf("%d. Triangle P1[%g,%g], P2[%g,%g], P3[%g,%g]\n", index, e->a->x, e->a->y, e->b->x, e->b->y, e->c->x, e->c->y);
		e = e->next;
	}
	printf("==============\n");
}

void freeTriangles(Triangle * list){
	if(list != NULL){
		freeTriangles(list->next);
		freeTriangle(list);
	}
}

void freeTriangleList(TriangleList * el){
	freeTriangles(el->triangles);
	free(el);
}

void addTriangle(TriangleList * el,  Triangle *e){
	el->triangles = addTriangleToList(el->triangles,e);
	el->length++;
}

void addAllTriangles(TriangleList * el1, TriangleList * el2){
    el1->triangles = addAllTrianglesToList(el1->triangles, el2->triangles);
    el1->length += el2->length;
}

void deleteTriangle(TriangleList * el, Triangle * e){
	el->triangles = removeTriangleFromList(el->triangles,e);
	el->length--;
}

int containsTriangle(TriangleList * el, Triangle * e){
	return containsTriangleInList(el->triangles,e);
}

void deleteTriangleByIndex(TriangleList * el, int index){
	el->triangles = removeTriangleByIndex(el->triangles,index);
	el->length--;
}

Triangle * getTriangle(TriangleList * tl, int idx){
	int i;
	Triangle * t = tl->triangles;
	for(i = 0; i < idx; i++)
		t = t->next;
	return t;
}

int countContainedEdges(TriangleList * list, Point * a, Point * b){
	Triangle * t = list->triangles;
	int counter = 0;
	while(t != NULL){
		if(triangleContainsPoint(t, a) && triangleContainsPoint(t, b))
			counter++;
		t = t->next;
	}
	return counter;
}

//int main(){
//    printf("Triangle_LIST");
//    TriangleList * el = newTriangleList();
//    int i=0;
//    for(i=0; i<20; i++){
//        Triangle * e = newTriangle();
//        addTriangle(el,e);
//    }
//    printf("ELEMENTS: %d", el->length);
//
//
//    freeTriangleList(el);
//    printf("FREE");
//}
